//package com.chixing.service.impl;
//
//import com.chixing.mapper.TravelSightsMapper;
//import com.chixing.pojo.TravelSights;
//import com.chixing.service.RecommendService;
//import org.apache.mahout.cf.taste.common.TasteException;
//import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
//import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
//import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
//import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
//import org.apache.mahout.cf.taste.model.DataModel;
//import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
//import org.apache.mahout.cf.taste.recommender.RecommendedItem;
//import org.apache.mahout.cf.taste.recommender.Recommender;
//import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
//import org.apache.mahout.cf.taste.similarity.UserSimilarity;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.List;
//@Service
//public class RecommendServiceImpl implements RecommendService {
//    @Autowired
//    private TravelSightsMapper travelSightsMapper;
//    @Autowired
//    private DataModel dataModel;
//    @Override
//    public List<TravelSights> getRecommentProductByUser(Integer custId, Integer howMany){
//        List<TravelSights> travelSightsList = null;
//        try {
//            UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
//            UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(100, similarity, dataModel);
//            Recommender recommender = new GenericUserBasedRecommender(dataModel, userNeighborhood, similarity);
//            long startTime = System.currentTimeMillis();
//            List<RecommendedItem> recommendedItemList = recommender.recommend(custId, howMany);
//            ArrayList<Integer> sightsIds = new ArrayList<>();
//            for (RecommendedItem recommendedItem:recommendedItemList
//                 ) {
//                sightsIds.add((int)recommendedItem.getItemID());
//            }
//            if(sightsIds!=null && sightsIds.size()>0){
//                travelSightsList = travelSightsMapper.selectAllByIds(sightsIds);
//            }else {
//                travelSightsList = new ArrayList<>();
//            }
//        } catch (TasteException e) {
//            e.printStackTrace();
//        }
//        return travelSightsList;
//    }
//
//    @Override
//    public List<TravelSights> getRecommentProductBySights(Integer custId, Integer sightsId, Integer howMany) {
//        List<TravelSights> travelSightsList = null;
//        try {
//            /*计算相似度，相似度的计算方式很多，采用基于皮尔逊相关性的相似度*/
//            ItemSimilarity itemSimilarity = new PearsonCorrelationSimilarity(dataModel);
//
//
//            /*构建推荐器，基于用户的协同过滤推荐*/
//            GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(dataModel, itemSimilarity);
//            long startTime = System.currentTimeMillis();
//            /*推荐商品*/
//            List<RecommendedItem> recommendedItemList = recommender.recommendedBecause(custId, sightsId, howMany);
//
//            List<Integer> sightsIds = new ArrayList<>();
//
//            for (RecommendedItem recommendedItem : recommendedItemList) {
//                sightsIds.add((int) recommendedItem.getItemID());
//            }
//
//            /*根据商品id 查询商品*/
//            if (sightsIds != null && sightsIds.size() > 0) {
//                travelSightsList = travelSightsMapper.selectAllByIds(sightsIds);
//            } else {
//                travelSightsList = new ArrayList<>();
//            }
//        } catch (TasteException e) {
//            e.printStackTrace();
//        }
//        return travelSightsList;
//    }
//}
